﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ReactiveCode;
using ReactiveCode.Observables;
using E = ReactiveCode.Expressions.ReactiveExpression;

namespace SolarSystem.Converted
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ReactiveHost _host = new ReactiveHost();

        public MainWindow()
        {
            InitializeComponent();

            _host.Call(GoGoPlanets());
        }

        ReactiveMethodDefinition<Unit> _goGoPlanets;
        ReactiveMethodDefinition<Unit> GoGoPlanets()
        {
            if (_goGoPlanets == null)
            {
                _goGoPlanets = new ReactiveMethodDefinition<Unit>(delegate
                {
                    var angle = new Variable<double>();
                    var earthCoords = new Variable<Point>();
                    var sunCoords = new Variable<Point>();
                    return E.Block(
                        E.Assignment(() => angle.Value, () => DateTime.Now.Precision(TimeSpan.FromMilliseconds(3)).TimeOfDay.TotalSeconds * 2 * 360 / 60 * Math.PI / 180),
                        E.Assignment(() => sunCoords.Value, () => new Point(Canvas.GetLeft(sun) + sun.Width / 2, Canvas.GetTop(sun) + sun.Height / 2)),
                        E.Assignment(() => earthCoords.Value, () => sunCoords.Value + new Vector
                        (
                            earthRadius.Value * Math.Cos(angle.Value),
                            earthRadius.Value * Math.Sin(angle.Value)
                        )),
                        E.Assignment(() => earth, Canvas.LeftProperty, () => earthCoords.Value.X - earth.Width / 2),
                        E.Assignment(() => earth, Canvas.TopProperty, () => earthCoords.Value.Y - earth.Height / 2),

                        E.Assignment(() => moon.Visibility, () => showMoon.IsChecked == true ? Visibility.Visible : Visibility.Collapsed),
                        E.Assignment(() => moon, Canvas.LeftProperty, () => earthCoords.Value.X + moonRadius.Value * Math.Cos(angle.Value * 12) - moon.Width / 2),
                        E.Assignment(() => moon, Canvas.TopProperty, () => earthCoords.Value.Y + moonRadius.Value * Math.Sin(angle.Value * 12) - moon.Width / 2)
                        );
                });
            }

            return _goGoPlanets;
        }

        private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var xy = Mouse.GetPosition((IInputElement)sender);
            Canvas.SetLeft(sun, xy.X - sun.Width / 2);
            Canvas.SetTop(sun, xy.Y - sun.Height / 2);
        }
    }
}
